package com.personal.controller;

import com.personal.annotation.PageView;
import com.personal.bo.AllQueryBO;
import com.personal.bo.NewResourcesBO;
import com.personal.constant.CarouselConstant;
import com.personal.enums.HomeEnum;
import com.personal.exception.ExceptionUtils;
import com.personal.mapper.ResourcesMapper;
import com.personal.pojo.Carousel;
import com.personal.pojo.Resources;
import com.personal.service.CarouselService;
import com.personal.service.DynamicService;
import com.personal.service.HomeService;
import com.personal.service.ResourcesService;
import com.personal.utils.Result;
import com.personal.vo.ResourcesVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("index")
@Api(tags = "首页展示相关接口")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private HomeService homeService;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private ResourcesService resourcesService;


    @ApiOperation("全站查询")
    @GetMapping("/allQuery")
    public Result allQuery(AllQueryBO allQueryBO){

        List<ResourcesVO> resourcesList = homeService.allQuery(allQueryBO);
        if (resourcesList.isEmpty()){
            ExceptionUtils.error(HomeEnum.QUERY_ALL_RESOURCES_IS_NULL);
        }

        return Result.success(resourcesList);
    }

    @ApiOperation("获取首页轮播图列表")
    @GetMapping("/carousel")
    public Result carousel(){
        List<Carousel> carouselList = carouselService.queryAllCarousel(CarouselConstant.IS_SHOW_TRUE);
        return Result.success(carouselList);
    }

    @GetMapping("/queryCatList")
    @ApiOperation("查询分类列表")
    public Result queryCatList(){
        return homeService.queryCatList();
    }

    @GetMapping("/queryDynamicList")
    @ApiOperation("查询动态表中的前200条数据")
    public Result queryDynamicList(){
        return homeService.queryDynamicList();
    }

    @GetMapping("/queryResourcesCount")
    @ApiOperation("查询今日，本周，总 发布资源数量")
    public Result queryResourcesCount(){
        return dynamicService.queryResourcesCount();
    }

    @GetMapping("/queryNewResources")
    @ApiOperation("查询最新的资源文章列表")
    public Result queryNewResources(@Valid NewResourcesBO newResourcesBO){
        return resourcesService.queryNewResources(newResourcesBO);
    }

    @PageView
    @GetMapping("/queryResourcesById")
    @ApiOperation("根据资源文章id获取资源文章详情")
    public Result queryResourcesById(@RequestParam("id") Integer id){
        return resourcesService.queryResourcesById(id);
    }


}
